//
//  ZDBWithdrawalViewController.m
//  ZDB
//
//  Created by Kk on 2020/3/24.
//  Copyright © 2020 ZDB. All rights reserved.
//

#import "ZDBWithdrawalViewController.h"
#import "ZDBBindingAlipayViewController.h"
@interface ZDBWithdrawalViewController ()<UITextFieldDelegate>
{
    BOOL isHaveDian;
}
@property(assign,nonatomic)NSInteger showNumber;
@end

@implementation ZDBWithdrawalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"提现";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self stepUI];
    [self getIsDrawingInfo];
}

-(void)stepUI{
    
    KViewRadius(self.bgView, 4);
    KViewRadius(self.bgView2, 4);
    KViewRadius(self.allBtn, 3);
    self.numberTF.delegate = self;
    self.numberTF.keyboardType = UIKeyboardTypeNumberPad;
    NSDictionary * userDic = [[DJLoginHelper sharedInstance] cj_UserDicc];
    self.nameLab.text = userDic[@"zfbName"];
    self.zhifubaoLab.text = userDic[@"zfbNumber"];
    
    
    
WEAKSELF
    [self.allBtn add_BtnClickHandler:^(NSInteger tag) {
        STRONGSELFFor(weakSelf);
        [strongSelf updateNumberTF];
    }];
    
    [self.editBtn add_BtnClickHandler:^(NSInteger tag) {
        STRONGSELFFor(weakSelf);
        ZDBBindingAlipayViewController * vcc = [ZDBBindingAlipayViewController new];
        vcc.title = @"编辑支付宝";
        vcc.updetaInfoClickBlock = ^(BOOL isUpdate) {
            [strongSelf updateZhifubao];
        };
        [strongSelf.navigationController pushViewController:vcc animated:YES];
        
     }];
    [self.sureBtn add_BtnClickHandler:^(NSInteger tag) {
        STRONGSELFFor(weakSelf);
        [strongSelf makeSureActionRequest];
    }];
    
    
    

}

-(void)makeSureActionRequest{
    //addDrawingInfoDrawingInfo
    
    
    if ([self.numberTF.text floatValue] ==0) {
        
        [JMNotifyView showNotify:@"提现金额不能为0" isSuccessful:NO];
        return;
    }
    
    if ([self.numberTF.text floatValue] >self.showNumber) {
           
           [JMNotifyView showNotify:@"提现金额不能大于可提款金额" isSuccessful:NO];
           return;
       }
     self.sureBtn.enabled = NO;
    [self showLoadingAnimation];
       WEAKSELF
             NSString  * getUserInfo = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"addDrawingInfoDrawingInfo"];
             NSMutableDictionary * dataDicc = [NSMutableDictionary dictionary];
             [dataDicc setValue:@([self.numberTF.text floatValue]) forKey:@"money"];
             [[SmileHttpTool sharedInstance] POST:getUserInfo parameters:dataDicc origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
                   STRONGSELFFor(weakSelf);
                 NSLog(@"--getIsDrawingInfo--%@",responseObject);
                 [self stopLoadingAnimation];
                 strongSelf.sureBtn.enabled = YES;
              
                 if (statusCode==0) {
                
                [JMNotifyView showNotify:@"提款成功，请关注提款信息" isSuccessful:YES];
                [strongSelf.navigationController popViewControllerAnimated:YES];
                 }else{
                     [JMNotifyView showNotify:message isSuccessful:NO];
                
                     
                 }
           
                 
             } failure:^(id responseObject, NSError *error) {
               STRONGSELFFor(weakSelf);
                  [self stopLoadingAnimation];
                strongSelf.sureBtn.enabled = YES;
             }];
    
}
-(void)updateZhifubao{
    
    NSDictionary * userDic = [[DJLoginHelper sharedInstance] cj_UserDicc];
    self.nameLab.text = userDic[@"zfbName"];
    self.zhifubaoLab.text = userDic[@"zfbNumber"];
    
}
-(void)updateNumberTF{
    
    self.numberTF.text = [NSString stringWithFormat:@"%ld",self.showNumber];
    
    
}
-(void)getIsDrawingInfo{
    [self showLoadingAnimation];
    WEAKSELF
          NSString  * getUserInfo = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"getIsDrawingInfo"];
          [[SmileHttpTool sharedInstance] POST:getUserInfo parameters:@{} origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
              NSLog(@"--getIsDrawingInfo--%@",responseObject);
              [self stopLoadingAnimation];
              STRONGSELFFor(weakSelf);
              if (statusCode==0) {
                 strongSelf.sureBtn.enabled = YES;
                  NSString * number = [NSString stringWithFormat:@"%@",responseObject];
                  strongSelf.showNumber = [number integerValue];
                  strongSelf.maneylab.text = [NSString stringWithFormat:@"%@元",number];
              }else{
                  [JMNotifyView showNotify:message isSuccessful:NO];
                  strongSelf.sureBtn.enabled = NO;
                  
              }
        
              
          } failure:^(id responseObject, NSError *error) {
            STRONGSELFFor(weakSelf);
               [self stopLoadingAnimation];
             strongSelf.sureBtn.enabled = NO;
          }];
    
    
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([textField.text rangeOfString:@"."].location == NSNotFound) {
       isHaveDian = NO;
     }
    if ([string length] > 0) {

        unichar single = [string characterAtIndex:0];//当前输入的字符
        if ((single >= '0' && single <= '9') || single == '.') {//数据格式正确

             //首字母不能为0和小数点
            if([textField.text length] == 0){
                if(single == '.') {
                    [self showError:@"亲，第一个数字不能为小数点"];
                    [textField.text stringByReplacingCharactersInRange:range withString:@""];
                    return NO;
                }
                 if (single == '0') {
                    [self showError:@"亲，第一个数字不能为0"];
                    [textField.text stringByReplacingCharactersInRange:range withString:@""];
                     return NO;
                 }
             }

            //输入的字符是否是小数点
            if (single == '.') {
                 if(!isHaveDian)//text中还没有小数点
                 {
                    isHaveDian = YES;
                     return YES;

                 }else{
                     [self showError:@"亲，您已经输入过小数点了"];
                     [textField.text stringByReplacingCharactersInRange:range withString:@""];
                     return NO;
                 }
             }else{
                 if (isHaveDian) {//存在小数点

                    //判断小数点的位数
                     NSRange ran = [textField.text rangeOfString:@"."];
                     if (range.location - ran.location <= 2) {
                        return YES;
                     }else{                         [self showError:@"亲，您最多输入两位小数"];
                        return NO;
                    }
                }else{
                     return YES;
                 }
             }
         }else{//输入的数据格式不正确
            [self showError:@"亲，您输入的格式不正确"];
             [textField.text stringByReplacingCharactersInRange:range withString:@""];
            return NO;
         }
    }
    else
     {
         return YES;
   }
}
 - (void)showError:(NSString *)errorString
{
    [JMNotifyView showNotify:errorString isSuccessful:NO];
  }

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
